﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ESpace.DalTools
{
	public partial class RandomString : Form
	{
		public RandomString()
		{
			InitializeComponent();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void RandomString_Load(object sender, EventArgs e)
		{
			ddlSourceString.SelectedIndex = 0;
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			string strSourceString = "abcdefghijklmnopqrstuvwxyz";
			if (ddlSourceString.SelectedIndex == 0)
			{
				strSourceString = "abcdefghijklmnopqrstuvwxyz";
			}
			else if (ddlSourceString.SelectedIndex == 1)
			{
				strSourceString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			}
			else if (ddlSourceString.SelectedIndex == 2)
			{
				strSourceString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
			}
			else if (ddlSourceString.SelectedIndex == 3)
			{
				strSourceString = "0123456789";
			}
			else if (ddlSourceString.SelectedIndex == 4)
			{
				strSourceString = "0123456789ABCDEF";
			}
			else if (ddlSourceString.SelectedIndex == 5)
			{
				strSourceString = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
			}
			else if (ddlSourceString.SelectedIndex == 6)
			{
				strSourceString = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~`!@#$%^&*()_+|[{]};:'\",<.>/?";
			}

			txtRandomString.Text = MakeRandomString(strSourceString, (int)txtCharCount.Value);
		}

		public static string MakeRandomString(string strFromChar, int intLength)
		{
			string sRandomString = "";

			Random rRand = new Random();
			for (int i = 0; i < intLength; i++)
			{
				int iIndex = rRand.Next(strFromChar.Length);
				sRandomString = sRandomString + strFromChar.Substring(iIndex, 1);
			}

			if (sRandomString.Length > intLength)
			{
				sRandomString = sRandomString.Substring(0, intLength);
			}
			return sRandomString;
		}

	}
}
